package com.example.system.class19;

/**
 * @Date 2022/8/12
 * @Author Jonathan
 */
public class Code02_CardsLine {
    public static void main(String[] args) {
        int[] arr = new int[]{20, 10, 100, 30};
        System.out.println(getMax(arr));

    }

    public static int getMax(int[] arr) {
        return Math.max(f(arr, 0, arr.length - 1), g(arr, 0, arr.length - 1));
    }


    // 先手获得的分数
    public static int f(int[] arr, int left, int right) {
        if (left == right) {
            return arr[left];
        }
        final int p1 = arr[left] + g(arr, left + 1, right);
        final int p2 = arr[right] + g(arr, left, right - 1);
        return Math.max(p1, p2);
    }

    // 后手获得的最好的分数
    public static int g(int[] arr, int left, int right) {
        if (left == right) {
            return 0;
        }
        final int p1 = f(arr, left + 1, right); // 对手拿了left
        final int p2 = f(arr, left, right - 1); // 对手拿了right
        return Math.min(p1, p2);  // 先手做选择  后手只能 被动选择
    }
}
